#!/usr/bin/env bash 
 
echo "start install mysql" 

# 用rpm添加MySQL程序包
rpm -ivh /home/downloads/mysql80-community-release-el7-1.noarch.rpm

yum install -y  mysql-community-server 

echo "启动并开机自启动MySQL程序..."
systemctl start mysqld
systemctl enable mysqld

echo "mysql 安装成功"
echo "开始测试 mysql 是否安装看成。看下面输出"
mysql -V 

# 修改密码
echo "修改密码"
tmpPwd = grep "A temporary password" /var/log/mysqld.log |cut -d : -f 4 |cut -d " " -f 2 | awk '{ print $1 }'
mysql -u root -p $tmpPwd
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Root123./';

# 可以通过以下命令再次登录
# mysql -uroot -pRoot123./

# MySQL默认的端口是3306，所有在防火墙中添加一个永久的端口
echo "开启防火墙端口"
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload

echo "end install mysql"